package com.mairuide._frame.utils.excellistener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.mairuide._frame.utils.SpringUtil;
import com.mairuide.excel_rule.pojo.ExcelRule;
import com.mairuide.excel_rule.service.ExcelRuleService;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 敲代码的卡卡罗特
 * on 2019/1/8 11:08.
 *  解析文件得到ExcelRule
 */
public class AnalysisExcelRuleExcelListener extends AnalysisEventListener {
    private  ExcelRule excelRule=null;
    //规则的service
    private ExcelRuleService excelRuleService;
    //数据库里面的标题头集合
    private List<String> headList=new ArrayList<>();
    //文件名
    private String filename;
    @Override
    public void invoke(Object object, AnalysisContext context) {
        try {
            if(this.excelRule!=null){
                return;
            }
            //如果超过10行就结束
            Integer rowNum = context.getCurrentRowNum();
            if(rowNum>10){
                return;
            }
            getExceltitleDictByTitleHead();
            List<String> s = (List<String>)object;
            s = s.stream().map(m -> {
                if (m!=null){
                    return m.replaceAll("\n|\n", "").trim();
                }else{
                    return null;
                }
            }).collect(Collectors.toList());
            System.out.println(s);
            //遇到空白行跳过
            String join = StringUtils.join(s, "");
            if (StringUtils.isBlank(join)) return;
            String titleString=StringUtils.join(s,",");
            System.out.println("标题头是：："+titleString);
            List<ExcelRule> excelRuleList = excelRuleService.getExcelRuleByTitleHead(titleString);
            if(excelRuleList!=null){
                if (excelRuleList.size()==1){
                    this.excelRule=excelRuleList.get(0);
                    return;
                }else{
                    for (int i = 0; i < excelRuleList.size(); i++) {
                        ExcelRule excelRule = excelRuleList.get(i);
                        if (filename.contains(excelRule.getTemplateName())){
                            this.excelRule=excelRule;
                            return;
                        }
                    }
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }
    public void doSomething(){

    }

    public void getExceltitleDictByTitleHead(){
        if (this.excelRuleService==null){
            ExcelRuleService excelRuleService = SpringUtil.getBean(ExcelRuleService.class);
            this.excelRuleService=excelRuleService;
        }
    }

    public ExcelRule getExcelRule() {
        return excelRule;
    }

    public void setExcelRule(ExcelRule excelRule) {
        this.excelRule = excelRule;
    }

    public ExcelRuleService getExcelRuleService() {
        return excelRuleService;
    }

    public void setExcelRuleService(ExcelRuleService excelRuleService) {
        this.excelRuleService = excelRuleService;
    }

    public List<String> getHeadList() {
        return headList;
    }

    public void setHeadList(List<String> headList) {
        this.headList = headList;
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }
}
